package com.ruoyi.jwxt.controller;

import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.jwxt.domain.DTO.JwxtLabDTO;
import com.ruoyi.jwxt.domain.PO.JwxtLab;
import com.ruoyi.jwxt.domain.PO.JwxtLabMaintenance;
import com.ruoyi.jwxt.utility.JwxtUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.jwxt.domain.DTO.JwxtLabMaintenanceDTO;
import com.ruoyi.jwxt.service.IJwxtLabMaintenanceService;

/**
 * 设备报修Controller
 */
@RestController
@RequestMapping("/jwxt/maintenance")
public class JwxtLabMaintenanceController extends BaseController
{
    @Autowired
    private IJwxtLabMaintenanceService jwxtLabMaintenanceService;

    /**
     * 查询设备报修列表
     */
    @PreAuthorize("@ss.hasPermi('jwxt:maintenance:list')")
    @GetMapping("/list")
    public TableDataInfo list(JwxtLabMaintenanceDTO jwxtLabMaintenanceDTO)
    {
        startPage();
        List<JwxtLabMaintenance> listPO=jwxtLabMaintenanceService.select(jwxtLabMaintenanceService.convert(jwxtLabMaintenanceDTO));
        listPO= JwxtUtils.maintenancesFilterByUser(listPO);
        TableDataInfo dataInfo=getDataTable(listPO);
        TableDataInfo tableDataCopy=new TableDataInfo();
        BeanUtils.copyProperties(dataInfo,tableDataCopy,"row");
        List<JwxtLabMaintenanceDTO> listDTO=listPO.stream().map(jwxtLabMaintenanceService::convert).collect(Collectors.toList());
        tableDataCopy.setRows(listDTO);

        return tableDataCopy;
    }

//     /**
//      * 导出设备报修列表
//      */
//     @PreAuthorize("@ss.hasPermi('jwxt:maintenance:export')")
//     @Log(title = "设备报修", businessType = BusinessType.EXPORT)
//     @PostMapping("/export")
//     public void export(HttpServletResponse response, JwxtLabMaintenanceDTO jwxtLabMaintenanceDTO)
//     {
//         List<JwxtLabMaintenanceDTO> list = jwxtLabMaintenanceService.select(jwxtLabMaintenanceDTO);
//         ExcelUtil<JwxtLabMaintenanceDTO> util = new ExcelUtil<JwxtLabMaintenanceDTO>(JwxtLabMaintenanceDTO.class);
//         util.exportExcel(response, list, "设备报修数据");
//     }

    /**
     * 获取设备报修详细信息
     */
    @PreAuthorize("@ss.hasPermi('jwxt:maintenance:query')")
    @GetMapping(value = "/{repairId}")
    public AjaxResult getInfo(@PathVariable("repairId") Long repairId)
    {
        return success(jwxtLabMaintenanceService.select(repairId));
    }

    /**
     * 新增设备报修
     */
    @PreAuthorize("@ss.hasPermi('jwxt:maintenance:add')")
    @Log(title = "设备报修", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JwxtLabMaintenanceDTO jwxtLabMaintenanceDTO)
    {
        return toAjax(jwxtLabMaintenanceService.insert(jwxtLabMaintenanceService.convert( jwxtLabMaintenanceDTO)));
    }

    /**
     * 修改设备报修
     */
    @PreAuthorize("@ss.hasPermi('jwxt:maintenance:edit')")
    @Log(title = "设备报修", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JwxtLabMaintenanceDTO jwxtLabMaintenanceDTO)
    {
        return toAjax(jwxtLabMaintenanceService.update(jwxtLabMaintenanceService.convert(jwxtLabMaintenanceDTO)));
    }

    /**
     * 删除设备报修
     */
    @PreAuthorize("@ss.hasPermi('jwxt:maintenance:remove')")
    @Log(title = "设备报修", businessType = BusinessType.DELETE)
	@DeleteMapping("/{repairIds}")
    public AjaxResult remove(@PathVariable Long[] repairIds)
    {
        return toAjax(jwxtLabMaintenanceService.delete(repairIds));
    }

}
